/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.cms.service;

import java.util.List;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.cms.dao.CategoryDao;
import com.thinkgem.jeesite.modules.cms.entity.Article;
import com.thinkgem.jeesite.modules.cms.entity.Category;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.modules.cms.entity.ExImage;
import com.thinkgem.jeesite.modules.cms.dao.ExImageDao;

/**
 * 图片新闻Service
 * @author 夏国强
 * @version 2016-05-25
 */
@Service
@Transactional(readOnly = true)
public class ExImageService extends CrudService<ExImageDao, ExImage> {
    @Autowired
    private ExImageDao imageDao;
    @Autowired
    private CategoryDao categoryDao;

	public ExImage get(String id) {
		return super.get(id);
	}
	
	public List<ExImage> findList(ExImage exImage) {
		return super.findList(exImage);
	}
	
	public Page<ExImage> findPage(Page<ExImage> page, ExImage exImage) {
        if (exImage.getCategory()!=null && StringUtils.isNotBlank(exImage.getCategory().getId()) && !Category.isRoot(exImage.getCategory().getId())){
            Category category = categoryDao.get(exImage.getCategory().getId());
            if (category==null){
                category = new Category();
            }
            category.setParentIds(category.getId());
            category.setSite(category.getSite());
            exImage.setCategory(category);
        }
        else{
            exImage.setCategory(new Category());
        }
        return super.findPage(page, exImage);
	}
	
	@Transactional(readOnly = false)
	public void save(ExImage exImage) {

        if (!UserUtils.getSubject().isPermitted("cms:article:audit")){
            exImage.setDelFlag(ExImage.DEL_FLAG_AUDIT);
        }
        // 如果栏目不需要审核，则将该内容设为发布状态
        if (exImage.getCategory()!=null&&StringUtils.isNotBlank(exImage.getCategory().getId())){
            Category category = categoryDao.get(exImage.getCategory().getId());
            if (!Global.YES.equals(category.getIsAudit())){
                exImage.setDelFlag(ExImage.DEL_FLAG_NORMAL);
            }
        }
		super.save(exImage);
	}
	
	@Transactional(readOnly = false)
	public void delete(ExImage exImage,Boolean isRe) {
        exImage.setDelFlag(Boolean.TRUE.equals(isRe) ? Article.DEL_FLAG_NORMAL : Article.DEL_FLAG_DELETE);
//        articleService.delete(article, isRe);
		super.delete(exImage);
	}

    public List<ExImage> getImgByCategoryId(String cateGoryId){ return imageDao.getImgByCategoryId(cateGoryId); }
	
}